package com.zuoban.shiro.chain;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zuoban.entity.base.BaseUserInfo;
import com.zuoban.mapper.BaseUserInfoMapper;
import com.zuoban.util.JwtUtil;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigInteger;

@Component
@Order(4)
public class MySQLTokenChain implements AuthChain{

    @Resource
    private BaseUserInfoMapper baseUserInfoMapper;

    @Resource
    private JwtUtil jwtUtil;

    @Override
    public BigInteger authentication(String token) {

        String userId = jwtUtil.getClaims(token).getClaims().get("userId").asString();
        QueryWrapper<BaseUserInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        BaseUserInfo baseUserinfo = baseUserInfoMapper.selectOne(wrapper);
        if (baseUserinfo == null){
            throw new RuntimeException("该token中未解析出用户，请重新登录");
        }
        return new BigInteger(userId);
    }
}
